package com.jmx.yp.service.employee;

import java.util.*;
import com.jmx.yp.dao.IBaseDao;
import com.jmx.yp.entity.GroupEmployee;
import com.jmx.yp.exception.ErrorCode;
import com.jmx.yp.exception.HandlerException;
import com.jmx.yp.util.Tools;

/**
 * 
 * 功能描述：
 *         IEmployeeService实现类，处理员工管理的业务逻辑
 * @author Jenny
 *
 * Date:2011-1-18 下午11:39:15
 */
public class EmployeeService implements IEmployeeService {
	private IBaseDao dao;
	
	public void setDao(IBaseDao dao){
		this.dao = dao;
	}
	
	/**
	 * 新增员工
	 * @param GroupEmployee 登录帐号唯一 
	 * @throws Exception
	 */
	public void addEmployee(GroupEmployee employee) throws Exception{
		if(!isUniqueLoginName(employee.getGrpemp_id()))
			throw new HandlerException(ErrorCode.ERR_1000);
                                      		
		dao.saveObject("com.jmx.yp.entity.GroupEmployeeMapper.addEmployee", employee);
	}
	
	/**
	 * 登录帐号是否唯一
	 * @param loginName
	 * @return 唯一返回true，否则返回false
	 * @throws Exception
	 */
	private boolean isUniqueLoginName(String loginName) throws Exception{
		boolean flag = true;
		
		Integer count = (Integer)dao.getObject(
				"com.jmx.yp.entity.GroupEmployeeMapper.isUniqueId", loginName);
		if(count > 0)
			flag = false;
		
		return flag;
	}
	
	/**
	 * 根据组织、员工编码及名称查询员工列表	 * 
	 * @param queryGroupCode
	 * @param queryGroupName
	 * @param queryEmployeeCode
	 * @param queryEmployeeName
	 * @return 返回MAP，包含两个参数：total, rows
	 * @throws Exception (pageNo-1)*pageSize , pageSize
	 */
	@SuppressWarnings("unchecked")
	public Map queryEmployeeList(String queryGroupCode, String queryGroupName, String queryEmployeeCode, String queryEmployeeName, int page) throws Exception{
		Map map = new HashMap(2);
		int pageSize = dao.getPageSize();
		
		//参数MAP
		Map paramMap = new HashMap();
		if(queryGroupCode != null && queryGroupCode.trim().length() > 0)
			paramMap.put("groupCode", queryGroupCode.trim());
		
		if(queryGroupName != null && queryGroupName.trim().length() > 0)
			paramMap.put("groupName", queryGroupName.trim());
		
		if(queryEmployeeCode != null && queryEmployeeCode.trim().length() > 0)
			paramMap.put("employeeCode", "%" + queryEmployeeCode.trim() + "%");
		
		if(queryEmployeeName != null && queryEmployeeName.trim().length() > 0)
			paramMap.put("employeeName", "%" + queryEmployeeName.trim() + "%");
		
		//总数
		int total = (Integer)dao.getObject("com.jmx.yp.entity.GroupEmployeeMapper.countQueryEmployee", paramMap);
		
		paramMap.put("pageSize", pageSize);
		paramMap.put("page", (page - 1) * pageSize);
		
		//LIST
		List list = dao.getList("com.jmx.yp.entity.GroupEmployeeMapper.queryEmployee", paramMap);
		if(page > 1 && (list == null || list.isEmpty())){ //当前页>1，查询又无结果，说明是以前的页数已经被删完。则自动显示最后一页
			page = Tools.computeTotalPage(total, pageSize);
			paramMap.put("page", (page - 1) * pageSize);
			list = dao.getList("com.jmx.yp.entity.GroupEmployeeMapper.queryEmployee", paramMap);
		}
		
		handleListTel(list);
		
		map.put("total", total); //总记录数
		map.put("rows", list);
		map.put("nowPage", page); //当前页
		map.put("pageTotal", Tools.computeTotalPage(total, pageSize));                    //总页数
		
		return map;
	}
	
	
	@SuppressWarnings("unchecked")
	private void handleListTel(List list){
		if(list == null || list.isEmpty())
			return;
		
		String temp = "";
		for(Iterator its = list.iterator(); its.hasNext();){
			Map tMap = (Map)its.next();
			
			if(tMap.get("GRPEMP_TEL_CN") != null){
				temp = (String)tMap.get("GRPEMP_TEL_CN") + "-";
			}
			
			if(tMap.get("GRPEMP_TEL_AREA") != null){
				temp = (String)tMap.get("GRPEMP_TEL_AREA") + "-";
			}
			
			if(tMap.get("GRPEMP_TEL") != null){
				temp = (String)tMap.get("GRPEMP_TEL");
			}
			
			tMap.remove("GRPEMP_TEL_CN");
			tMap.remove("GRPEMP_TEL_AREA");
			
			tMap.put("GRPEMP_TEL", temp);
			temp = "";
		}
	}
	
	/**
	 * 根据ID查询员工
	 * @param serialid
	 * @return
	 * @throws Exception
	 */
	public GroupEmployee getEmployeeById(Long serialid) throws Exception{
		GroupEmployee employee = (GroupEmployee)dao.getObject("com.jmx.yp.entity.GroupEmployeeMapper.getEmployeeById", serialid);
		return employee;
	}
	
	/**
	 * 修改员工信息
	 * @param employee
	 * @throws Exception
	 */
	public void updateEmployee(GroupEmployee employee) throws Exception{
		dao.updateObject("com.jmx.yp.entity.GroupEmployeeMapper.updateEmployee", employee);
	}
	
	/**
	 * 批量删除员工
	 *    备注：为逻辑删除，标志位置状态
	 *      
	 * @param employeeIds 多个ID时以;作为分隔
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public void deleteEmployee(String employeeIds, String operator) throws Exception{
		if(employeeIds == null || employeeIds.trim().length() <= 0)
			return;
		
		Map map = new HashMap(3);
		map.put("array", employeeIds.split(";"));
		map.put("operator", operator);
		map.put("oTime", new Date());
		
		dao.updateObject("com.jmx.yp.entity.GroupEmployeeMapper.deleteEmployee", map);
	}
	
	public static void main(String[] args){
		Map map = new HashMap();
		
		map.put("2", 2);
		
		map.remove("e");
		
		System.out.println("end");
		
		int pagesize = 15;
		int row = 29;
		int pageTotal = row / pagesize;
		if(row % pagesize > 0)
			pageTotal += 1;
		
		System.out.println(pageTotal);
	}
	
}
